<?php
session_start();
require_once('session.php');
require_once '../sql/connection.php';
//记录的总条数
$total_num = mysqli_num_rows(mysqli_query($GLOBALS['conn'],"SELECT * FROM commodity"));
//每页记录数
$pagesize = 10;
//总页数
$page_num = ceil($total_num / $pagesize);
//设置页数
$page = isset($_GET['page']) ? $_GET['page'] : 1;
if ($page < 1 || $page == '') {
    $page = 1;
}
if ($page > $page_num) {
    $page = $page_num;
}
//计算机记录的偏移量
$offset = $pagesize * ($page - 1);
//上一页、下一页
$prepage = ($page <> 1) ? $page - 1 : $page;
$nextpage = ($page <> $page_num) ? $page + 1 : $page;
$sql_config = "select * from commodity";
// 连接数据库，并查询
$result_config = mysqli_query($GLOBALS['conn'], $sql_config);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>无标题文档</title>
    <link href="css/table.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td class="tt" colspan="5">产品列表</td>
    </tr>
    <tr>
        <td width="6%" height="35">ID</td>
        <td>商品名</td>
        <td width="29%">价格</td>
        <td colspan="2">库存</td>
    </tr>
    <?php
    if ($total_num > 0) {
        while ($row = mysqli_fetch_array($result_config)) {
            ?>
            <tr>
                <td height="31"><?php echo $row['0'] ?></td>
                <td height="31"><?php echo $row['1'] ?></td>
                <td height="31"><?php echo $row['3'] ?></td>
                <td height="31"><?php echo $row['4'] ?></td>
            </tr>
            <?php
        }
    } else {
        echo "<tr><td colspan='5' height='31' style='color:red;font-size:13px'>暂无记录</td></tr>";
    }
    mysqli_close($GLOBALS['conn']);
    ?>
    <tr>
        <td height="43" colspan="6" align="center">
            <?= $page ?>/<?= $page_num ?>&nbsp;&nbsp;<a href="?page=1">首页</a>&nbsp;&nbsp; <a
                    href="?page=<?= $prepage ?>">上一页</a>&nbsp;&nbsp;<a href="?page=<?= $nextpage ?>">下一页</a>&nbsp;&nbsp;<a
                    href="?page=<?= $page_num ?>"> 尾页</a>
        </td>
    </tr>
</table>
</body>
</html>
